<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.supplierName"
        clearable
        class="filter-item"
        style="width: 200px;"
        placeholder="输入供应商名称"
      />
      <el-input
        v-model="listQuery.supplierContacts"
        clearable
        class="filter-item"
        style="width: 200px;"
        placeholder="联系人"
      />
      <el-input
        v-model="listQuery.supplierPhone"
        clearable
        class="filter-item"
        style="width: 200px;"
        placeholder="联系电话"
      />
      <el-select v-model="listQuery.status" clearable style="width: 200px" class="filter-item" placeholder="请选择状态" >
        <el-option v-for="(item,index) in statusMap" :key="index" :label="item.name" :value="item.value" />
      </el-select>
      <el-button
        v-permission="['supplier:supplier:list']"
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >查找</el-button>
      <el-button
        v-permission="['supplier:supplier:create']"
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >添加</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      size="small"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
      style="white-space: pre-line"
    >
      <el-table-column prop="supplierNo" align="center" label="供应商编号"/>
      <el-table-column prop="supplierName" align="center" label="供应商名称"/>
      <el-table-column prop="supplierContacts" align="center" label="联系人姓名"/>
      <el-table-column prop="supplierPhone" align="center" label="电话"/>
      <el-table-column prop="supplierAddress" align="center" label="地址"/>

      <el-table-column prop="status" align="center" label="状态" ><template slot-scope="scope">
        <el-tag>{{ scope.row.status === 0? '禁用': '正常' }}</el-tag>
      </template></el-table-column>
      <el-table-column align="center" label="操作" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-permission="['supplier:supplier:update']"
            v-if="scope.row.status=='1'"
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>
          <el-button
            v-permission="['supplier:supplier:detail']"
            type="info"
            size="mini"
            @click="handleDetail(scope.row)"
          >查看</el-button>
          <el-button
            v-permission="['supplier:supplier:active']"
            v-if="scope.row.status=='1'"
            type="danger"
            size="mini"
            @click=" handleStatus(scope.row)">冻结</el-button>
          <el-button
            v-permission="['supplier:supplier:active']"
            v-else
            type="primary"
            size="mini"
            @click="handleStatus(scope.row)"
          >激活</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNo"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
    <!-- 详情对话框 -->
    <el-dialog :visible.sync="supplierDialogVisible" title="详情">
      <el-form
        :data="supplierDetail"
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="编号">
          <span>{{ supplierDetail.supplierNo }}</span>
        </el-form-item>
        <el-form-item label="名称">
          <span>{{ supplierDetail.supplierName }}</span>
        </el-form-item>
        <el-form-item label="地址">
          <span>{{ supplierDetail.supplierAddress }}</span>
        </el-form-item>
        <el-form-item label="联系人姓名">
          <span>{{ supplierDetail.supplierContacts }}</span>
        </el-form-item>
        <el-form-item label="电话">
          <span>{{ supplierDetail.supplierPhone }}</span>
        </el-form-item>
        <el-form-item label="邮箱">
          <span>{{ supplierDetail.supplierEmail }}</span>
        </el-form-item>
        <el-form-item label="银行">
          <span>{{ supplierDetail.supplierBank }}</span>
        </el-form-item>
        <el-form-item label="银行账号">
          <span>{{ supplierDetail.supplierBankAccount }}</span>
        </el-form-item>
        <el-form-item label="税点" >
          <span>{{ supplierDetail.supplierTax }}</span>
        </el-form-item>
        <el-form-item label="描述">
          <span>{{ supplierDetail.supplierInfo }}</span>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 添加或修改对话框 -->
    <el-dialog :title="textMap[dialogStatus]" :custom-class="dialogClass" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="dataForm"
        status-icon
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left:50px;"
      >

        <el-form-item label="编号" prop="supplierNo">
          <el-input v-model="dataForm.supplierNo" clearable placeholder=""/>
        </el-form-item>
        <el-form-item label="名称" prop="supplierName">
          <el-input v-model="dataForm.supplierName" clearable placeholder=""/>
        </el-form-item>
        <el-form-item label="地址" prop="supplierAddress">
          <el-input v-model="dataForm.supplierAddress" clearable placeholder=""/>
        </el-form-item>
        <el-form-item label="联系人姓名" prop="supplierContacts">
          <el-input v-model="dataForm.supplierContacts" clearable placeholder=""/>
        </el-form-item>
        <el-form-item label="电话" prop="supplierPhone">
          <el-input v-model="dataForm.supplierPhone" clearable placeholder=""/>
        </el-form-item>
        <el-form-item label="邮箱" prop="supplierEmail">
          <el-input v-model="dataForm.supplierEmail" clearable placeholder=""/>
        </el-form-item>
        <el-form-item label="银行" prop="supplierBank">
          <el-input v-model="dataForm.supplierBank" clearable placeholder=""/>
        </el-form-item>
        <el-form-item label="银行账号" prop="supplierBankAccount">
          <el-input v-model="dataForm.supplierBankAccount" clearable placeholder=""/>
        </el-form-item>
        <el-form-item label="税点" prop="supplierTax">
          <el-input v-model="dataForm.supplierTax" clearable placeholder="">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="描述" prop="supplierInfo">
          <el-input v-model="dataForm.supplierInfo" type="textarea" clearable placeholder=""/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
        <el-button v-else type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
.el-table .cell {
  white-space: pre-line;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 120px;
  height: 120px;
  display: block;
}
</style>

<script>
import { list, create, del, update, active, detail } from '@/api/supplier'
import { formatDateAndTime } from '@/filters'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
export default {
  name: 'Supplier',
  components: { Pagination },
  filters: {
    formatGmt(time) {
      if (time == null || time === '') {
        return '无'
      }
      return formatDateAndTime(time)
    }
  },
  data() {
    return {
      statusMap: [{ value: 0, name: '禁用' }, { value: 1, name: '正常' }],
      list: undefined,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        limit: 20,
        status: undefined
      },
      options: [],
      dataForm: {
        id: undefined
      },
      supplierDetail: {},
      supplierDialogVisible: false,
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        supplierNo: [{ required: true, message: '供应商编号不能为空', trigger: 'blur' }],
        supplierName: [{ required: true, message: '供应商名称不能为空', trigger: 'blur' }],
        supplierContacts: [{ required: true, message: '联系人姓名不能为空', trigger: 'blur' }],
        supplierPhone: [{ required: true, message: '电话不能为空', trigger: 'blur' }],
        supplierAddress: [{ required: true, message: '地址不能为空', trigger: 'blur' }],
        supplierEmail: [{ type: 'email', message: '请输入正确的邮箱地址' }]
      },
      dialogClass: 'dialogClass'
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      list(this.listQuery)
        .then(response => {
          this.list = response.data.data.items
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        id: undefined
      }
    },
    handleStatus(row) {
      this.resetForm()
      this.dataForm.id = row.id
      this.dataForm.status = row.status === 1 ? 0 : 1
      active(this.dataForm)
        .then(res => {
          this.$notify.success({
            title: '成功',
            message: '状态更改成功'
          })
          this.getList()
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      // this.refreshOptions()
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          create(this.dataForm)
            .then(response => {
              this.getList()
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '创建成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: '失败'
              })
            })
        }
      })
    },
    handleDetail(row) {
      detail(row).then(response => {
        this.supplierDetail = response.data.data
      })
      this.supplierDialogVisible = true
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          update(this.dataForm)
            .then(response => {
              this.getList()
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: '失败'
              })
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: '失败'
            })
          })
      }).catch(() => {
        return false
      })
    }
  }
}
</script>

